<template>
    <view class="my-date-picker">
        <view class="input" @click="show = true">
            <view class="text">
                {{ dayjs(value).format(mode === 'date' ? 'YYYY-MM-DD' : 'YYYY-MM-DD hh:mm:ss') }}
            </view>
            <u-icon name="calendar" color="#ccc"></u-icon>
        </view>
        <u-datetime-picker :show="show" v-model="localValue" :mode="mode" :closeOnClickOverlay="true" @confirm="confirm"
            @cancel="cancel" @close="close"></u-datetime-picker>
    </view>
</template>

<script>
    export default {
        name: 'DatePicker',
        props: {
            value: [Number, String],
            mode: {
                type: String,
                default: 'date'
            }
        },
        data() {
            return {
                localValue: Number(new Date()),
                show: false,
            }
        },
        methods: {
            confirm(current) {
                this.show = false;
                this.$emit('input', current.value);
                this.$emit('change', current.value);
            },
            cancel() {
                this.show = false;
            },
            close() {
                this.show = false;
            }
        },
        mounted() {
            this.localValue = this.value;
        }
    };
</script>

<style lang="scss" scoped>
    .my-date-picker {
        height: 8rem;
        width: 100%;
        position: relative;

        .input {
            position: absolute;
            width: 100%;
            height: 100%;
            top: 0;
            left: 0;
            display: flex;
            justify-content: space-between;
            align-items: center;
            border: 1px solid #ccc;
            border-radius: 1rem;
            padding: 0 2rem;
            font-size: 2rem;

            /deep/ .u-icon__icon {
                font-size: 4rem !important;
                line-height: 4rem !important;
            }
        }

    }
</style>